/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tongxunlu;

import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author Tim
 */
public class CSVOP {
    public static boolean CSVReader(String File,ArrayList<person> al){
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(File),"GBK"));

            String[] line = new String[27];
            String getline = null;
            br.readLine();
            while ((getline = br.readLine()) != null) {
             do{
                 getline = getline.concat(",无");
                 line = getline.split(",");
             }while(line.length<27);

              person stu = new person();
              String name = line[0];
                stu.setNAME(name);
                stu.setTEL(line[1].equals("")?"无":line[1]);
                stu.setEMAIL(line[2].equals("")?"无":line[2]);
                stu.setBIR(line[3].equals("")?"无":line[3]);
                stu.setGROUP(line[4].equals("")?"无":line[4]);
                stu.setREMARKS(line[5].equals("")?"无":line[5]);
                
                stu.getWORKMASSAGE().setWORKCOUNTRY(line[6].equals("")?"无":line[6]);
                stu.getWORKMASSAGE().setWORKPROVINCE(line[7].equals("")?"无":line[7]);
                stu.getWORKMASSAGE().setWORKCITY(line[8].equals("")?"无":line[8]);
                stu.getWORKMASSAGE().setWORKADD(line[9].equals("")?"无":line[9]);
                stu.getWORKMASSAGE().setWORKUNIT(line[10].equals("")?"无":line[10]);
                stu.getWORKMASSAGE().setDEPARTMENT(line[11].equals("")?"无":line[11]);
                stu.getWORKMASSAGE().setPOSITION(line[12].equals("")?"无":line[12]);
                stu.getWORKMASSAGE().setWORKZIPCODE(line[13].equals("")?"无":line[13]);
                stu.getWORKMASSAGE().setWORKTEL(line[14].equals("")?"无":line[14]);
                stu.getWORKMASSAGE().setWORKFAX(line[15].equals("")?"无":line[15]);
                
                stu.getNETMASSAGE().setHOMEPAGE(line[16].equals("")?"无":line[16]);
                stu.getNETMASSAGE().setQQNUM(line[17].equals("")?"无":line[17]);
                stu.getNETMASSAGE().setMSNNUM(line[18].equals("")?"无":line[18]);
                stu.getNETMASSAGE().setBACKUPEMAIL(line[19].equals("")?"无":line[19]);
                
                stu.getHOMEMASSAGE().setHOMECOUNTRY(line[20].equals("")?"无":line[20]);
                stu.getHOMEMASSAGE().setHOMEPROVINCE(line[21].equals("")?"无":line[21]);
                stu.getHOMEMASSAGE().setHOMECITY(line[22].equals("")?"无":line[22]);
                stu.getHOMEMASSAGE().setHOMEADD(line[23].equals("")?"无":line[23]);
                stu.getHOMEMASSAGE().setHOMEZIPCODE(line[24].equals("")?"无":line[24]);
                stu.getHOMEMASSAGE().setHOMETEL(line[25].equals("")?"无":line[25]);
                stu.setPHOTOADD(line[26].equals("")?"无":line[26]);
                
                al.add(stu);//在这加上插入函数！！！！
            }
        if(al.isEmpty()){
            br.close();
            return false;
        }
        }catch(IOException e){e.printStackTrace();}
        return true;
    }
    
    
    
   public static boolean CSVWriter(String File,ArrayList<person> al) throws IOException{
        try{
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(File),"GBK"));
            bw.write("姓名"+","+"电话"+","+"邮箱"+","+"生日"+","+"所属组"+","+"备注"+","
                    +"工作国家"+","+"工作省份"+","+"工作城市"+","+"工作地址"+","+"工作单位"+","+"工作部门"+","+"职位"+","+"邮政编码"+","+"工作电话"+","+"传真"+","
                    +"个人主页"+","+"QQ号码"+","+"MSN号码"+","+"备用邮箱"+","
                    +"家所在国家"+","+"家所在省份"+","+"家所在城市"+","+"家庭地址"+","+"家庭邮编"+","+"家庭电话"+"照片地址");
            for(int i = 0;i<al.size();i++){              
                bw.newLine();
                bw.write(al.get(i).getNAME() +","+al.get(i).getTEL()+","+al.get(i).getEMAIL()+","+al.get(i).getBIR()+","+al.get(i).getGROUP()+","+al.get(i).getREMARKS()+","
                        +al.get(i).getWORKMASSAGE().getWORKCOUNTRY() +","+ al.get(i).getWORKMASSAGE().getWORKPROVINCE()+","+al.get(i).getWORKMASSAGE().getWORKCITY()+","+al.get(i).getWORKMASSAGE().getWORKADD()+","+al.get(i).getWORKMASSAGE().getWORKUNIT()+","+al.get(i).getWORKMASSAGE().getDEPARTMENT()+","+al.get(i).getWORKMASSAGE().getPOSITION()+","+al.get(i).getWORKMASSAGE().getWORKZIPCODE()+","+al.get(i).getWORKMASSAGE().getWORKTEL()+","+al.get(i).getWORKMASSAGE().getWORKFAX()+","
                        +al.get(i).getNETMASSAGE().getHOMEPAGE()+","+al.get(i).getNETMASSAGE().getQQNUM()+","+al.get(i).getNETMASSAGE().getMSNNUM()+","+al.get(i).getNETMASSAGE().getBACKUPEMAIL()+","
                        +al.get(i).getHOMEMASSAGE().getHOMECOUNTRY()+","+al.get(i).getHOMEMASSAGE().getHOMEPROVINCE()+","+al.get(i).getHOMEMASSAGE().getHOMECITY()+","+al.get(i).getHOMEMASSAGE().getHOMEADD()+","+al.get(i).getHOMEMASSAGE().getHOMEZIPCODE()+","+al.get(i).getHOMEMASSAGE().getHOMETEL()+","+al.get(i).getPHOTOADD());
                }
            bw.close();
            }catch(IOException e){e.printStackTrace();
            return false;}
        return true;
    }
//    public static void main(String[] args) throws IOException {
//        ArrayList<person> al = new ArrayList<person>();
//        person p1 = new person("潘洪添","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p2 = new person("潘洪添2","223456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p3 = new person("潘洪添3","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国4","广东","广州","华农","519640","020-1235124")));
//        person p4 = new person("潘洪添4","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p5 = new person("潘洪添5","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p6 = new person("潘洪添6","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p7 = new person("潘洪添7","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
//        person p8 = new person("潘洪添8","23456778901","238785748@qq.com","19921105","同学","无","panhongtian.jpg",
//                (new workmassage("中国","广东","广州","华农","信息学院","10计机3","学生","519640","4474473","0756-23124")),
//                (new netmassage("www.tuasdfjkl.com","278583647","123asdga","23893759872@qq.com")),
//                (new homemassage("中国","广东","广州","华农","519640","020-1235124")));
////        al.add(p8);
////        al.add(p7);
////        al.add(p6);
////        al.add(p5);
////        al.add(p4);
////        al.add(p3);
////        al.add(p2);
////        al.add(p1); 
//        CSVReader("test.csv",al);
//        CSVWriter("test1.csv", al);
//        System.out.println(al.get(1).getNAME());
//        System.out.println(al.get(2).getNAME());
//        System.out.println(al.get(3).getNAME());
//        System.out.println(al.get(4).getNAME());
//        System.out.println(al.get(5).getNAME());
//        return ;
//    }
    
}
